; autostart.asm
; Autostart area code for FE3 ram disk

	.include "rdglobal.inc"

	.export init_auto
	.export restvec

	.import __MODULE_LOAD__
	.import __MODULE_RUN__
	.import __MODULE_SIZE__
	
	.import rd_open
	.import rd_close
	.import rd_save
	.import rd_load
	.import rd_chkin
	.import rd_ckout
	.import rd_clrch
	.import rd_clall
	.import nmi_handler
	.import rd_nmi
	
	VECTOR_BASE = $0314

	.segment "MODULE"

a0cbm:		.word rdreset
			.word rda0nmi
			.byte $41, $30, $C3, $C2, $CD

;
;	Reset handler
;

rdreset:	ldx #$FF
			sei
			txs
			cld
			jsr $FD8D			; Detect RAM
			jsr $FD52			; Set I/O vectors
			jsr restvec
			jmp $FD35

rda0nmi:	jmp rd_nmi

restvec:	ldx #0
@vecloop:	lda vectbl, x
			bmi @done
			tay
			inx
			lda vectbl, x
			sta VECTOR_BASE, y
			inx
			lda vectbl, x
			sta VECTOR_BASE+1, y
			inx
			bne @vecloop
@done:		rts
			
vectbl:		.byte OPEN_VECTOR - VECTOR_BASE, <rd_open, >rd_open
			.byte CLOSE_VECTOR - VECTOR_BASE, <rd_close, >rd_close
			.byte LOAD_VECTOR - VECTOR_BASE, <rd_load, >rd_load
			.byte SAVE_VECTOR - VECTOR_BASE, <rd_save, >rd_save
			.byte CHKIN_VECTOR - VECTOR_BASE, <rd_chkin, >rd_chkin
			.byte CKOUT_VECTOR - VECTOR_BASE, <rd_ckout, >rd_ckout
			.byte CLRCH_VECTOR - VECTOR_BASE, <rd_clrch, >rd_clrch
			.byte CLALL_VECTOR - VECTOR_BASE, <rd_clall, >rd_clall
			.byte NMI_VECTOR - VECTOR_BASE, <nmi_handler, >nmi_handler
			.byte $FF

			
	.segment "BOOT"
	
; copy code to autostart area (must be less than 256 bytes for now).
init_auto:	ldx #0
@cploop:	lda __MODULE_LOAD__, x
			sta __MODULE_RUN__,x 
			inx
			cpx #<__MODULE_SIZE__
			bcc @cploop 
			rts
